Java Demystified by James Keogh
Author:James Keogh
Language: eng
Format: epub, pdf
Publisher: McGraw-Hill Education
Published: 2004-01-20T16:00:00+00:00
Overriding Method Members Using Inheritance
Previously you learned that a method member enables an instance of a class to perform a specific kind of behavior, such as displaying instance variables on the screen. A subclass inherits behavior from a superclass when a subclass can access method members of the superclass. You saw this in the previous example, where the GraduateStudent class called method members of its superclass to display instance variables.
Sometimes the behavior of a method member of a superclass doesn't meet the needs of a subclass. For example, the manner in which a superclass's method member displays an instance variable isn't exactly the way the subclass wants the instance variable displayed.
In this case, a programmer defines another version of the superclass's method member within the subclass and includes statements that enhance the original behavior of the superclass's method member. Programmers referred to this as overriding a method member.
Don't confuse overriding a method member with overloading a method, which you learned about in Chapter 6. Overloading a method requires you to define a method that has the same method name but a different argument list than a method that is already defined. That is, each method has a different signature. Overriding a method member requires you to use the same method name and the same argument list as a method member defined in a subclass's superclass.
You might think that having two method members with the same signature will confuse Java. It won't, and here's why: Java uses method members of the class whose instances call the method. Therefore, if you use an instance of the subclass in your program to call the method member, Java uses the subclass's version of the method member.
Let's take a look at an example to see how this works. The following program is a variation of the program used in the previous example. Here's what is happening in this program: three classes are defined. They are the Person class, the Student class, and the GraduateStudent class. The Person class and the Student class both declare an instance variable, initialize the instance variable, and define a method member called display() that displays its instance variable. Notice that these instance variables are designed with the protected access specifier. This means they can be accessed directly by the subclass.
Neither version of the display() method member is suitable for the GraduateStudent class. Therefore, the GraduateStudent class overrides the display() method member. Statements within the new version of the display() method directly access instance variables of the Person class and the Student class.
An instance of the GraduateStudent class is declared in the main() method, and the instance is used to call the display() method. Java uses the display() method member defined in the GraduateStudent class.
class Demo { public static void main (String args[]) { GraduateStudent gs = new GraduateStudent (); gs.display(); } } class Person { protected String name; Person(){ name = "Bob Smith"; } void display(){ System.out.println("Person Class: " + name); } } class Student extends Person { protected int studentID; Student (){ studentID = 12345; } void display(){ System.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
ActiveX | ASP.NET |
Cold Fusion | CSS |
DHTML | Java Server Pages |
JavaScript | PHP |
Python | Ruby |
XSL |
Hello! Python by Anthony Briggs(9911)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9336)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Svelte with Test-Driven Development by Daniel Irvine(7116)
Test-Driven Development with PHP 8 by Rainier Sarabia(6842)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6705)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Web Development with Django by Ben Shaw Saurabh Badhwar(6175)
React Application Architecture for Production by Alan Alickovic(5895)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5806)
Kotlin in Action by Dmitry Jemerov(5062)
Audition by Ryu Murakami(4583)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4427)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4276)
Functional Programming in JavaScript by Mantyla Dan(4037)
